<template>
  <div class="container-box">
    <div class="list hidebars">
      <ul class="hidebars">
        <li
          v-for="(item,index) in routerList"
          :key="index"
          @click="toPath(item.path)"
          v-if="item.meta.title !== '首页'"
        >
          <div>{{item.meta.title}}</div>
        </li>
      </ul>
    </div>
    <div class="view">
      <div class="title">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item
            v-for="(item,index) in breadList"
            :key="index"
            :to="{ path: item.path }"
          >{{item.meta.title || item.name}}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="container">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "home",
  data() {
    return {
      routerList: [],
      breadList: [] // 路由集合
    };
  },

  created() {
    this.getBreadcrumb();
    this.setRouter();
  },
  watch: {
    $route() {
      this.getBreadcrumb();
    }
  },

  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched;
      // console.log(matched);

      let arr = [];

      arr.push(this.$route.matched);
      //如果不是首页
      // if (!this.isHome(matched[0])) {
      //   matched = [{ path: "/home", meta: { title: "首页" } }].concat(matched);
      // }

      this.breadList = matched;
      // console.log(this.breadList);
    },
    setRouter() {
      // console.log(this.$router.options.routes);
      this.routerList = this.$router.options.routes[0].children;
    },
    toPath(n) {
      this.$router.push(n);
    }
  }
};
</script>

<style lang="scss" scoped>
.container-box {
  display: flex;
  .list {
    width: 210px;
    overflow: hidden;
    // position: absolute;
    ul {
      height: 100vh;
      overflow-y: scroll;
      display: flex;
      flex-direction: column;
      li {
        height: 56px;
        line-height: 56px;
        font-size: 14px;
        padding: 0 20px;
        cursor: pointer;
        color: rgb(191, 203, 217);
        background-color: rgb(48, 65, 86);
      }
    }
  }
  .view {
    height: 100vh;
    overflow-y: scroll;
    flex-grow: 1;
    .title {
      padding: 0 20px;
    }
    .container {
      padding: 20px;
      height: 100%;
      box-sizing: border-box;
    }
  }
}

.el-breadcrumb /deep/ {
  line-height: 56px;
}
</style>

